package com.itany.cloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author 石小俊
 */
//@Component
public class PrintFilterParameterGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return new GatewayFilter() {
            /**
             * 定义过滤规则
             * @param exchange  可以通过该对象获取到web相关的信息
             * @param chain 用于过滤器放行
             * @return
             */
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                // 读取过滤器工厂中配置的参数名与参数值
                System.out.println(config.getName() + ":" + config.getValue());
                return chain.filter(exchange);
            }
        };
    }
}
